package cn.edu.scut.sys.controller;

import java.sql.Timestamp;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.scut.sys.service.DepartmentService;
import cn.edu.scut.sys.service.StudentService;
import cn.edu.scut.sys.service.UnitService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.utils.StringUtils;
import cn.edu.scut.sys.vo.Department;
import cn.edu.scut.sys.vo.Student;
import cn.edu.scut.sys.vo.Unit;
import cn.edu.scut.sys.vo.User;

@Controller
@RequestMapping("/student")
public class StudentController {

	private StudentService studentService;

	@Resource(name="studentService")
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}
	
	private DepartmentService departmentService;
	
	@Resource(name="departmentService")
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	
	private UnitService unitService;
	
	@Resource(name="unitService")
	public void setUnitService(UnitService unitService) {
		this.unitService = unitService;
	}
	
	/**
	 * 
	 * @param page
	 * @param rows
	 * @param sidx
	 * @param sord
	 * @param search
	 * @param searchField
	 * @param searchOper
	 * @param searchString
	 * @param filters
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public DataResponse<Student> list(@RequestParam(defaultValue="1",value="page") String page,  
            @RequestParam(defaultValue="20",value="rows") String rows,  
            @RequestParam("sidx") String sidx,  
            @RequestParam("sord") String sord,  
            @RequestParam("search") boolean search,  
            @RequestParam(required=false,value="searchField") String searchField,  
            @RequestParam(required=false,value="searchOper") String searchOper,  
            @RequestParam(required=false,value="searchString") String searchString,  
            @RequestParam(required=false,value="filters") String filters,
            HttpServletRequest request
            ){  
		 try {  
	            DataRequest dataRequest = new DataRequest();  
	            dataRequest.setPage(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));  
	            dataRequest.setRows(StringUtils.isEmpty(rows) ? 20 : Integer.valueOf(rows));  
	            dataRequest.setSidx(sidx);  
	            dataRequest.setSord(sord);  
	            dataRequest.setSearch(search);  
	            dataRequest.setSearchField(searchField);  
	            dataRequest.setSearchOper(searchOper);  
	            dataRequest.setSearchString(searchString);  
	            return studentService.list(dataRequest, request);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        return null;  
	}
	
	@RequestMapping("/add")
	public String add(String name, String registerTime, String sex, 
			String departmentId, String unitByNowGroupId, String unitByNewTeamId, String unitByNowClassId,
			String status, HttpServletRequest request){
		try{
			// Department
			Department department = new Department();
			department.setId(Long.parseLong(departmentId));
			
			// unitByNowGroup
			Unit unitByNowGroup = new Unit();
			unitByNowGroup.setId(Long.parseLong(unitByNowGroupId));
			
			// unitByNewTeam
			Unit unitByNewTeam = new Unit();
			unitByNewTeam.setId(Long.parseLong(unitByNewTeamId));
			
			// unitByNowClass
			Unit unitByNowClass = new Unit();
			unitByNowClass.setId(Long.parseLong(unitByNowClassId));
			
			// student
			Student student = new Student();
			student.setName(name);
			student.setRegisterTime(Timestamp.valueOf(registerTime + " 00:00:00"));
			student.setSex(Integer.parseInt(sex));
			student.setDepartment(department);
			student.setUnitByNowGroup(unitByNowGroup);
			student.setUnitByNewTeam(unitByNewTeam);
			student.setUnitByNowClass(unitByNowClass);
			student.setStatus(Integer.parseInt(status));
			studentService.add(student);
			request.setAttribute("status", 1);
			return "/page/studentAdd";
		} catch(Exception ex) {
			request.setAttribute("status", 0);
			return "/page/studentAdd";
		}
	}
	
	@RequestMapping("/toAdd")
	public String toAdd(HttpServletRequest request){
		request.setAttribute("departmentList", departmentService.listAll());
		request.setAttribute("unitByNowGroupList", unitService.listByUnitId(1L));
		return "/page/studentAdd";
	}
	
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable(value = "id") Long id, HttpServletRequest request){
		Student student = studentService.get(id);
		request.setAttribute("student", student);
		request.setAttribute("departmentList", departmentService.listAll());
		request.setAttribute("unitByNowGroupList", unitService.listByUnitId(1L));
		if (null != student.getUnitByNowGroup()){
			request.setAttribute("unitByNewTeamList", unitService.listByUnitId(student.getUnitByNowGroup().getId()));
			if (null != student.getUnitByNewTeam()){
				request.setAttribute("unitByNowClassList", unitService.listByUnitId(student.getUnitByNewTeam().getId()));
			}
		}
		return "/page/studentEdit";
	}
	
	@RequestMapping("/update")
	public String update(String id, String name, String registerTime, String sex, 
			String departmentId, String unitByNowGroupId, String unitByNewTeamId, String unitByNowClassId,
			String status, HttpServletRequest request){
		try{
			// Department
			Department department = new Department();
			department.setId(Long.parseLong(departmentId));
						
			// unitByNowGroup
			Unit unitByNowGroup = new Unit();
			unitByNowGroup.setId(Long.parseLong(unitByNowGroupId));
				
			// unitByNewTeam
			Unit unitByNewTeam = new Unit();
			unitByNewTeam.setId(Long.parseLong(unitByNewTeamId));
						
			// unitByNowClass
			Unit unitByNowClass = new Unit();
			unitByNowClass.setId(Long.parseLong(unitByNowClassId));
			
			// student
			Student student = studentService.get(Long.parseLong(id));
			student.setId(Long.parseLong(id));
			student.setName(name);
			student.setRegisterTime(Timestamp.valueOf(registerTime + " 00:00:00"));
			student.setSex(Integer.parseInt(sex));
			student.setDepartment(department);
			student.setUnitByNowGroup(unitByNowGroup);
			student.setUnitByNewTeam(unitByNewTeam);
			student.setUnitByNowClass(unitByNowClass);
			student.setStatus(Integer.parseInt(status));
			studentService.update(student);
			request.setAttribute("status", 1);
			return "/page/studentEdit";
		} catch(Exception ex) {
			request.setAttribute("status", 0);
			return "/page/studentEdit";
		}
	}
	
	@RequestMapping("/setUserPage/{id}")
	public String setUserPage(@PathVariable(value = "id") Long id, HttpServletRequest request){
		request.setAttribute("student", studentService.get(id));
		return "/page/studentUserList";
	}
	
	@RequestMapping("/setUser")
	public String setUser(Long userId, Long studentId, HttpServletRequest request){
		try{
			Student student = studentService.get(studentId);
			User user = new User();
			user.setId(userId);
			student.setUser(user);
			studentService.update(student);
			return "/page/studentList";
		}catch(Exception ex){
			return "/page/studentList";
		}
	}
	
	@RequestMapping("/setUserNull")
	public String setUserNull(Long studentId, HttpServletRequest request){
		try{
			Student student = studentService.get(studentId);
			student.setUser(null);
			studentService.update(student);
			return "/page/studentList";
		}catch(Exception ex){
			return "/page/studentList";
		}
	}
	
}
